package com.one.login.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.one.login.common.RedisUtil;
import com.one.login.pojo.vo.LoginTicket;
import com.one.login.pojo.vo.User;
import com.one.login.service.UserService;

@Controller
@RequestMapping(value = "intent", method = { RequestMethod.GET })
public class IntentController {

	@Autowired
	private HttpSession session;

	@Autowired
	private HttpServletResponse response;

	@Autowired
	private UserService userService;

	@Autowired
	private RedisUtil redisUtil;

	@RequestMapping(value = "/login.html")
	public String login() {
		return "login";
	}

	@RequestMapping(value = "/checkCookie")
	public void checkCookie(@RequestParam String url) throws IOException {
		String userId = (String) session.getAttribute("userId");
		// 已登录
		if (userId != null) {
			String st = (String) session.getAttribute("st");
			// st存入redis里
			// ******************
			LoginTicket loginTicket = new LoginTicket();
			// 获取用户
			User user = userService.selectOneById(userId);
			// 设置用户
			loginTicket.setUser(user);
			//
			redisUtil.setObject(LoginTicket.class, loginTicket, "one-login", st);
			// ******************
			response.sendRedirect(url + "?st=" + st);
			return;
		}
		// 未登录
		session.setAttribute("url", url);
		response.sendRedirect("login.html");
		return;
	}

}
